/*
* $Id: ChooseList.java 239 2011-01-02 21:13:07Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import javax.microedition.lcdui.*;

import apiary.Apiary;
import apiary.ui.BackScreen;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;

public class ChooseList extends BackScreen implements ItemEventListener,
                                               ItemStateListener,
                                               CommandListener
{
    CategoryItem chooseItem;
    CategoryItem curItem;

    public ChooseList(String name, Item item) {
        super(name);
        chooseItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        addCommand(new Command(apiary.getLocaleString("Choose"), Command.BACK, 0));
        addCommand(new Command(apiary.getLocaleString("Cancel"), Command.CANCEL, 1));
        setCommandListener(this);
        setItemStateListener(this);
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(PRESS)){
            curItem = (CategoryItem) item;
            OnOk();
        }
    }

    public void itemStateChanged(Item item) {
        CategoryItem categoryItem = (CategoryItem) item;
        if(categoryItem.getActive())
            curItem = categoryItem;
    }

    public void commandAction(Command c, Displayable d) {
        if(c.getCommandType() == Command.BACK)
            OnOk();
        if(c.getCommandType() == Command.CANCEL)
            Apiary.getInstance().Display(backForm);
    }

    private void OnOk() {
        if(curItem != null) {
            chooseItem.setName(curItem.getName());
            chooseItem.setState(curItem.getId());
        }
        Apiary.getInstance().Display(backForm);
    }
}
